package integracion.Venta;

import java.io.*;
import java.util.ArrayList;

import negocio.Venta.*;

public class DAOVentaImp implements DAOVenta{
	private int lastId;
	
	public DAOVentaImp() 
	{ 	  
	  try
	  {
		this.lastId=-1;
		File archivo=new File("C:/cardealer/Venta/index.txt");
		if(archivo.exists())
		 	{
			 BufferedReader bfr = new BufferedReader(new FileReader(archivo));
			 this.lastId=Integer.parseInt(bfr.readLine());		
			 bfr.close();
		 	}
		 else
		 	{
			File directorio = new File("c:/cardealer/Venta/");
			directorio.mkdirs();			
			PrintWriter pw = new PrintWriter( new FileWriter((archivo)));			
			pw.println(this.lastId);
			pw.close();
		 	}			
	  }catch(IOException e)
	  {
		 e.printStackTrace();
	  }
	}
	
	public boolean create(TransferVenta transfer)
	{
	 Boolean resultado=false;
	 try
	 	{
		 lastId++;
		 File archivo= new File("c:/cardealer/Venta/" + Integer.toString(lastId) + ".txt");
		 File index = new File ("c:/cardealer/Venta/index.txt");
	
		 PrintWriter pw = new PrintWriter( new FileWriter((archivo)));
		 PrintWriter pwindex = new PrintWriter( new FileWriter((index)));
		
		 pw.println("true");
		 pw.println(transfer.getCliente());
		 pw.println(transfer.getFecha());
		 pw.println(transfer.getPrecio());
		 for(int i=0;i<transfer.getNumeroLineasVenta();i++)
		    {
			 pw.println(transfer.getLineaVenta(i).getVehiculo());
			 pw.println(transfer.getLineaVenta(i).getNumeroVehiculos());
			 pw.println(transfer.getLineaVenta(i).getPrecioUdad());
		    }		 
		 pwindex.println(lastId);
		 pw.close();
		 pwindex.close();			
		 resultado = true;
    	}catch(Exception e)
	    { e.printStackTrace();}
    return resultado;	
	}
	
	public boolean delete(TransferVenta transfer)
	{
		File archivo = new File("c:/cardealer/Venta/"+transfer.getId()+".txt");
		try
		{				
		 BufferedReader bfr = new BufferedReader(new FileReader(archivo));
		 bfr.readLine();			 
		 String cliente=bfr.readLine();
		 String fecha=bfr.readLine();
		 String precio=bfr.readLine();
		 String linea;
		 ArrayList<String> arraylineas = new ArrayList<String>();
		 while((linea=bfr.readLine())!=null)
		 	{
			 arraylineas.add(linea);
		 	}	 
		
		 PrintWriter pw = new PrintWriter( new FileWriter((archivo)));	
		 pw.println("false");
		 pw.println(cliente);
		 pw.println(fecha);
		 pw.println(precio);
		 for(int i=0;i<arraylineas.size();i++)
		 	{
			 pw.println(arraylineas.get(i));
		 	}
		 bfr.close();
		 pw.close();
		 
		 return true;		 
		}catch(Exception e){
			e.printStackTrace();
		 }
	   return false; 
	}	
	
	public boolean update(TransferVenta transfer)
	{
	  File archivo=new File("C:/cardealer/Venta/"+transfer.getId()+".txt");				
		try
		  {			
			PrintWriter pw = new PrintWriter( new FileWriter((archivo)));					
			 pw.println("true");
			 pw.println(transfer.getCliente());
			 pw.println(transfer.getFecha());
			 pw.println(transfer.getPrecio());
			 for(int i=0;i<transfer.getNumeroLineasVenta();i++)
			    {
				 pw.println(transfer.getLineaVenta(i).getVehiculo());
				 pw.println(transfer.getLineaVenta(i).getNumeroVehiculos());
				 pw.println(transfer.getLineaVenta(i).getPrecioUdad());
			    }		 
			 
			pw.close();	
			return true;								
		  }catch(Exception e){
		    	e.printStackTrace();
		     }	
		return false;
	}
	
	public TransferVenta readById(int id)
	{
	  File archivo= new File("c:/cardealer/Venta/" + Integer.toString(id) + ".txt");
	  TransferVenta transferventa=null;		
		try
		{		
		 BufferedReader bfr = new BufferedReader(new FileReader(archivo));	
		 bfr.readLine();
		 transferventa = new TransferVenta(id,
		 								   Integer.parseInt(bfr.readLine()),
		 								   bfr.readLine(),
		 								   Float.parseFloat(bfr.readLine()));
		 String linea;
		 while((linea=bfr.readLine())!=null)
		 	{
			 TransferLineaVenta lineaVenta =  new TransferLineaVenta(Integer.parseInt(linea),
					 												 Integer.parseInt(bfr.readLine()),
					 												 Float.parseFloat(bfr.readLine()));
			 transferventa.addLineaVenta(lineaVenta);
		 	} 
		 bfr.close();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		return transferventa;
	}	

	public boolean deletable(TransferVenta transfer)
	{
	 if(transfer.getId()<0 || transfer.getId()>lastId) return false;
	 if(!checkFlag(transfer.getId()))return false;
	 return true;
	}
	
	public boolean updatable(TransferVenta transfer)
	{
		if(transfer.getId()<0 || transfer.getId()>lastId) return false;
		if(checkFlag(transfer.getId())) return true;
		return false;
	}
	
	public boolean searchable(int id)
	{
	 if(id<0 || id>lastId)return false;
	 if(!checkFlag(id)) return false;
	 return true;
	}
	
	public boolean containsCliente(int id)
	{
	try
	  	{
		for(int i=0;i<=lastId;i++)
			{
			File archivo= new File("c:/cardealer/Venta/" + Integer.toString(i) + ".txt");
			BufferedReader bfr = new BufferedReader(new FileReader(archivo));
			bfr.readLine();
			if(id==Integer.parseInt(bfr.readLine()))
				return true;
			}
	}catch(Exception e)
	  	{
	  		e.printStackTrace();
	  	}
		return false;
	}
	
	public boolean containsVehiculo(int id)
	{
	 try
	  	{
		for(int i=0;i<=lastId;i++)
			{
			File archivo= new File("c:/cardealer/Venta/" + Integer.toString(i) + ".txt");
			BufferedReader bfr = new BufferedReader(new FileReader(archivo));
			bfr.readLine();
			bfr.readLine();
			bfr.readLine();
			bfr.readLine();
			String linea;
			while((linea=bfr.readLine())!=null)
				{
				 if(id==Integer.parseInt(linea)) return true;
				 bfr.readLine();
				 bfr.readLine();
				}			
			}
	}catch(Exception e)
	  	{
	  		e.printStackTrace();
	  	}
		return false;
	}
	
	private boolean checkFlag (int id)
	{		
	   File archivo= new File("c:/cardealer/Venta/" + Integer.toString(id) + ".txt");
		{
		  try{
			  BufferedReader bfr = new BufferedReader(new FileReader(archivo));	 
			  return Boolean.parseBoolean(bfr.readLine());
		  	 }catch(Exception e)
		  	 {e.printStackTrace();			
		  	 }
		 } 	 
		return false;
	}
}
